/******************* 页面数据载入 *******************/
var resId = core.getUrlData('res_id'),
	$balance = $('html body .wrap .invite-cash .pay .con div em'),
	$totalPay = $('html body .wrap .invite-cash .detail .con strong em'),
	$showBalance = $('html body .wrap .invite-cash section .con div strong'),
	$pay = $('html body .wrap .invite-cash .need span strong'),
	// $pay = $('html body .wrap .invite-cash .need span strong, html body .wrap .invite-cash .real .con strong em'),
	$needPay = $('html body .wrap .invite-cash .need span strong'),
	$realPay = $('html body .wrap .invite-cash .real .con strong em'),
	isPay = true;
	
(resId == 0) ? $('title, html body .header-title h1').html('诚意金支付') : $('title, html body .header-title h1').html('收银台')

$.ajax({
	url: payDataUrl,
	type: 'post',
	data: {
		id: resId
	},
	success: function(data) {
		console.log(data)
		// data.yue_money = 0;
		if (data.status == 1) {
			urlData = core.getUrlData(['res_id', 'res_money', 'res_time', 'my_yu_money'])
			// $balance.html('¥' + data.yue_money);
			$showBalance.html('帐户余额' + data.yue_money + '元');
			$totalPay.html('¥' + data.money)
			$pay.html('¥' + data.money);
			$needPay.html('¥' + data.money);
			$realPay.html('¥' + data.money);

			/******************* 公共变量 *******************/
			var bill = Number(core.subString($totalPay.html(), 0, 1)),
				balance = Number(data.yue_money),
				$payMethod = $('html body .wrap .invite-cash .pay-method .con div'),
				useBalance = false,
				payMethod,
				needPay = bill;

			/******************* 按钮功能 *******************/
			if (balance > 0) {
				// 余额抵扣
				core.addSelection({
					ele: 'html body .wrap .invite-cash .pay .con div',
					event: 'tap',
					multiple: true
				}, function($this, index, isActive) {
					useBalance = isActive;
					if (isActive) {
						needPay = bill - balance;
						if (needPay > 0) {
							$pay.html('¥' + needPay);
							$balance.html('¥' + core.decimalRound(balance, 2));
						} else {
							$pay.html('¥0');
							$balance.html('¥' + core.decimalRound(bill, 2));
							$payMethod.removeClass('active');
							payMethod = undefined;
						}
					} else {
						$pay.html('¥' + bill);
						$balance.html('¥0');
					}
				});
			} else {
				$('html body .wrap .invite-cash .pay .con div').on('tap', function () {
					core.layerHint('余额为0，不可抵扣哦~');
				});
			}

			// 支付类型
			$payMethod.on('tap', function () {
				var $this = $(this);
				if (useBalance) {
					if (needPay > 0) {
						$(this).addClass('active').siblings().removeClass('active');
						payMethod = $(this).attr('pay-method');
					} else {
						core.layerHint('余额充足，无需选择第三方支付');
						payMethod = undefined;
					}
				} else {
					if ($this.hasClass('active')) {
						$(this).removeClass('active').siblings().addClass('active');
						payMethod = undefined;
					} else {
						$(this).addClass('active').siblings().removeClass('active');
						payMethod = $(this).attr('pay-method');
					}
				}
			});

			// 支付按钮
			$('html body .wrap .invite-cash .btn').on('tap', function () {
				if (useBalance) {
					if (needPay > 0) {
						if (payMethod) {
							// console.log('合并')
							jiesuan({
								id: urlData[0],
								pay_type: 2
							});
						} else {
							core.layerHint('余额不足，请选择支付方式');
						}
					} else {
						// console.log('余额')
						common.layerConfirm({
							content: '您确定余额抵扣' + bill + '元支付' + ((resId == 0) ? '诚意金？' : '活动红包？'),
							bacClose: false,
							yes: function () {
								toPay({
									id: urlData[0],
									pay_type: 0
								});
							},
							end: function () {
								// isPay = true;
							}
						});
					}
				} else {
					if (payMethod) {
						// console.log('微信')
						jiesuan({
							id: urlData[0],
							pay_type: 1
						});
					} else {
						core.layerHint('请选择支付方式');
					}
				}
			});

			function toPay(payData) {
				if (isPay) {
					isPay = false;
					// common.layerConfirm({
						// content: '<h2>请输入支付密码</h2><div><span></span><span></span><span></span><span></span><span></span><span></span><input type="password" name="" value=""></div>',
						// className: 'layer-password',
						// btn: ['支付', '取消'],
						// yesClose: false,
						// yes: function (index) {
							// var inputVal = $('body .layui-m-layer .layui-m-layermain .layui-m-layersection .layer-password .layui-m-layercont input').val();
							// if (inputVal) {
								// if (core.pwJudge(inputVal, 'num')) {
									// payData.password = inputVal;
									layer.open({
										type: 2,
										shadeClose: false,
										className: 'loading',
										content: '请稍候',
										success: function () {
											$.ajax({
												url: ((payData.id == 0) ? chengyiPay : payUrl),
												type: 'post',
												data: payData,
												success: function(data) {
													console.log(data)

													if (data.status == 1) {
														if (payData.pay_type == 0) {
															(payData.id == 0) ? core.hintBack(data.message) : core.hintHref(data.message, indexUrl);
														} else if (payData.pay_type == 1) {
															
														} else if (payData.pay_type == 2) {
															
														}
													} else {
														isPay = true;
														layer.closeAll();
														core.layerHint(data.message);
													}
												}
											});
										}
									});
								// } else {
									// isPay = true;
									// core.layerHint('请输入6位纯数字密码');
								// }
							// } else {
								// isPay = true;
								// core.layerHint('输入密码不可为空');
							// }
						// },
						// no: function (index) {
							// isPay = true;
						// },
						// success: function (ele) {
							// $('body .layui-m-layer .layui-m-layermain .layui-m-layersection .layer-password .layui-m-layercont div input').focus().on('input', function () {
								// if ($(this).val().length == 6) {
									// $(this).blur();
								// }
							// });
						// },
						// end: function () {
							// isPay = true;
						// }
					// });
				} else {
					core.layerHint('您已经支付过该订单，不可以重复支付');
				}
			}
		} else {
			core.hintBack(data.message);
		}
	}
});


